<?php

/**
 *
 * @author Administrator
 */
class PageController {
	// TODO - Insert your code here
	private $path;
	private $lan;

	/**
	 */
	function __construct() {

		$this->path = "src/module/pc_view/page/temp/";
		if (isset( $_REQUEST[PARAM_LAN])) {
			$this->lan = $_REQUEST[PARAM_LAN];
		} else {
			$this->lan = DEFAULT_LANGAUGE;
		}
	}
	
	private function subMenuItem($fid){
		
		$subMenu = " ";
		
		// create data acces object
		$db = new DBAccess();
		
		// categories
		$list = $db->selectManyByCause( $GLOBALS['articleFullColArray'], ARTICLES, array(ARTICLE_CATEGORYID => $fid), array(ARTICLE_UPDATETIME => DESC));

		if (count($list) == 0){
			return " ";
		}
		
		$subMenu .= "<ul>";
		foreach ( $list as $item ) {
			$item[ARTICLE_URL] = createURL( $this->lan, ARTICLES, $item[ARTICLE_URL]);
			$subMenu .= "<li><a href=\"" . $item[ARTICLE_URL] . "\" title=\"" . $item[ARTICLE_TITLE] . "\">" . $item[ARTICLE_TITLE] . "</a></li>";
		}
		$subMenu .= "</ul>";
		
		// return page
		return $subMenu;
	}

	/**
	 * View main menu
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewSubMenu($type = CATEGORY_TYPE_PRODUCT, $fid = 0) {

		$subMenu = " ";
		
		// create data acces object
		$db = new DBAccess();
		
		if ($type == CATEGORY_TYPE_PRODUCT){
			// categories
			$list = $db->selectManyByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_FID => $fid, SQL_AND, CATEGORY_TYPE => $type, SQL_AND, CATEGORY_LANGUAGE => $this->lan), array(CATEGORY_POSITION => ASC));
			
			$subMenu .= "<ul>";
			foreach ( $list as $item ) {
				$item[CATEGORY_URL] = createURL( $this->lan, CATEGORIES, $item[CATEGORY_URL]);
				$subMenu .= "<li><a href=\"" . $item[CATEGORY_URL] . "\" title=\"" . $item[CATEGORY_TITLE] . "\">" . $item[CATEGORY_TITLE] . "</a></li>";
			}
			$subMenu .= "</ul>";
		} else if ($type == CATEGORY_TYPE_ACRTICLE){
			
			// categories
			$list = $db->selectManyByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_FID => $fid, SQL_AND, CATEGORY_TYPE => $type, SQL_AND, CATEGORY_LANGUAGE => $this->lan), array(CATEGORY_POSITION => ASC));
			
			$subMenu .= "<ul>";
			foreach ( $list as $item ) {
				$item[CATEGORY_URL] = createURL( $this->lan, CATEGORIES, $item[CATEGORY_URL]);
				$subMenu .= "<li><a href=\"" . $item[CATEGORY_URL] . "\" title=\"" . $item[CATEGORY_TITLE] . "\">" . $item[CATEGORY_TITLE] . "</a>". $this->subMenuItem($item[CATEGORY_ID]) ."</li>";
			}
			$subMenu .= "</ul>";
			
		} else if ($type == CATEGORY_TYPE_COLLECTION){
			// categories
			$list = $db->selectManyByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_FID => $fid, SQL_AND, CATEGORY_TYPE => $type, SQL_AND, CATEGORY_LANGUAGE => $this->lan), array(CATEGORY_POSITION => ASC));
				
			$subMenu .= "<ul>";
			foreach ( $list as $item ) {
				$item[CATEGORY_URL] = createURL( $this->lan, CATEGORIES, $item[CATEGORY_URL]);
				$subMenu .= "<li><a href=\"" . $item[CATEGORY_URL] . "\" title=\"" . $item[CATEGORY_TITLE] . "\">" . $item[CATEGORY_TITLE] . "</a></li>";
			}
			$subMenu .= "</ul>";
		}
		
		
		
		// return page
		return $subMenu;
	}

	/**
	 * View main menu
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewSupportBox($type = CATEGORY_TYPE_DOMESTIC) {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/supportbox.html");
		
		// create data acces object
		$db = new DBAccess();
		$list = $db->selectManyByCause( $GLOBALS['supportFullColArray'], SUPPORTERS, null, array(SUPPORT_POSITION => ASC));
		
		foreach ( $list as $item ) {
			$template->assign( ITEM, $item);
			$template->parse( SUPPORTBOX . "." . ITEM);
		}
		
		$contact = $this->getContactInfo();
		$template->assign( ORGANINFO_HOTLINE, $contact[ORGANINFO_HOTLINE]);
		
		// return page
		$template->parse( SUPPORTBOX);
		return $template->out( SUPPORTBOX);
	}

	private function getPageImage($cate = null, $table = null) {
		
		

		if ($cate == null && $table != null && $table!= FILES) {
			$db = new DBAccess();
			$item = $db->selectOneByCause( array(PRODUCT_CATEGORYID), $table, array(PRODUCT_URL => $_REQUEST[PARAM_URL]));
			$cate = $db->selectOneByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_ID => $item[PRODUCT_CATEGORYID]));
		} else if ($cate == null && $table == null || $table== FILES) {
			return array(CATEGORY_PAGEIMAGE => "upload/images/slide/default.jpg", CATEGORY_METAKEYWORD => "abc");
		}
		
		if (isset( $cate[CATEGORY_PAGEIMAGE]) && ! empty( $cate[CATEGORY_PAGEIMAGE])) {
			return $cate;
		} else {
			$db = new DBAccess();
			while ( $cate != null && (! isset( $cate[CATEGORY_PAGEIMAGE]) || empty( $cate[CATEGORY_PAGEIMAGE])) && ! empty( $cate[CATEGORY_FID]) ) {
				
				$cate = $db->selectOneByCause( $GLOBALS['categoryFullColArray'], CATEGORIES, array(CATEGORY_ID => $cate[CATEGORY_FID]));
			}
			
			if ( $cate != null && isset( $cate[CATEGORY_PAGEIMAGE]) && ! empty( $cate[CATEGORY_PAGEIMAGE]) ) {
				return $cate;
			} else {
				return array(CATEGORY_PAGEIMAGE => "upload/images/slide/default.jpg", CATEGORY_METAKEYWORD => "abc");
			}
		}
	
	}

	/**
	 * View nivo slider
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewMainSlider() {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/slidebox.html");
		
		$pageType = identifyURL();
		
		if ($pageType[MODULE] == HOMEPAGE ) {
			// create data acces object
			$db = new DBAccess();
			$list = $db->selectManyByCause( $GLOBALS['pageimageFullColArray'], PAGEIMAGES, array(PAGEIMAGE_TYPE => PAGEIMAGE_TYPE_MAINSLIDE, SQL_AND, PAGEIMAGE_LANGUAGE => $this->lan), array(PAGEIMAGE_POSITION => ASC));
			
			foreach ( $list as $item ) {
				$template->assign( ITEM, $item);
				$template->parse( SLIDERBOX . "." . NIVOSLIDER . "." . ITEM);
			}
			
			$template->parse( SLIDERBOX . "." . NIVOSLIDER);
		} else if ($pageType[MODULE] == "SEARCH"){
			
			$template->assign( ITEM, $this->getPageImage( null, null));
				
			$template->parse( SLIDERBOX . "." . PAGEIMAGE . "." . ITEM);
			$template->parse( SLIDERBOX . "." . PAGEIMAGE);
			
		} else {
			
			$template->assign( ITEM, $this->getPageImage( $pageType[CATEGORY], $pageType[MODULE]));
			
			$template->parse( SLIDERBOX . "." . PAGEIMAGE . "." . ITEM);
			$template->parse( SLIDERBOX . "." . PAGEIMAGE);
		}
		
		// return page
		$template->parse( SLIDERBOX);
		return $template->out( SLIDERBOX);
	}
	
	/**
	 * View nivo slider
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewProductImageSlider($product) {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/product-image-slide.html");
		
		
		// create data acces object
		$db = new DBAccess();
		$list = $db->selectManyByCause( $GLOBALS['productImageFullColArray'], PRODUCTIMAGES, array(PRODUCTIMAGE_PID => $product[PRODUCT_ID]), array(PAGEIMAGE_POSITION => ASC));
		
		if (count($list) > 0){
			$template->assign( IMAGE, $list[0]);
			$template->parse( SLIDERBOX . "." . IMAGE);
		} else {
			$template->assign( IMAGE, $product);
			$template->parse( SLIDERBOX . "." . IMAGE);
			$item = array();
			$item[PRODUCTIMAGE_IMAGELINK] = $product[PRODUCT_IMAGELINK];
			$item[PRODUCTIMAGE_IMAGEALT] = $product[PRODUCT_METAKEYWORD];
			$template->assign( ITEM, $item);
			$template->parse( SLIDERBOX . "." . ITEM);
		}
		
		
		for ($i = 0; $i < 5 && $i < count($list); $i++) {
			$template->assign( ITEM, $list[$i]);
			$template->parse( SLIDERBOX . "." . ITEM);
		}
		
		// return page
		$template->parse( SLIDERBOX);
		return $template->out( SLIDERBOX);
	}

	/**
	 *
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewRightBanner() {
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/rightbanner.html");
		
		// create data acces object
		$db = new DBAccess();
		$list = $db->selectManyByCause( $GLOBALS['pageimageFullColArray'], PAGEIMAGES, array(PAGEIMAGE_TYPE => PAGEIMAGE_TYPE_BANNERRIGHT, SQL_AND, PAGEIMAGE_LANGUAGE => $this->lan), array(PAGEIMAGE_POSITION => ASC));
		
		foreach ( $list as $item ) {
			$template->assign( ITEM, $item);
			$template->parse( RIGHTBANNER . "." . ITEM);
		}
		
		// return page
		$template->parse( RIGHTBANNER);
		return $template->out( RIGHTBANNER);
	}
	
	/**
	 * RIGHT BANNER
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewBottomSilde() {
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/bottomslide.html");
	
		// create data acces object
		$db = new DBAccess();
		$list = $db->selectManyByCause( $GLOBALS['pageimageFullColArray'], PAGEIMAGES, array(PAGEIMAGE_TYPE => PAGEIMAGE_TYPE_PARTNERLOGO, SQL_AND, PAGEIMAGE_LANGUAGE => $this->lan), array(PAGEIMAGE_POSITION => ASC));
	
		foreach ( $list as $item ) {
			$template->assign( ITEM, $item);
			$template->parse( SLIDERBOX . "." . ITEM);
		}
	
		// return page
		$template->parse( SLIDERBOX);
		return $template->out( SLIDERBOX);
	}

	/**
	 *
	 * @return string
	 */
	public function viewBanner() {
		
		// get id
		$db = new DBAccess();
		$item = $db->selectOneByCause( $GLOBALS['pagepopupFullColArray'], PAGEPOPUP, array(PAGEPOPUP_TYPE => PAGEPOPUP_TYPE_BANNER, SQL_AND, PAGEPOPUP_LANGUAGE => $this->lan));
		
		if ($item[PAGEPOPUP_FILETYPE] == "Image") {
			return "<a href=\"" . $item[PAGEPOPUP_LINK] . "\"><img src=\"" . $item[PAGEPOPUP_FILELINK] . "\" alt=\"" . $item[PAGEPOPUP_IMAGEALT] . "\" ></a>";
		} else {
			return " ";
		}
	
	}

	/**
	 * View popup
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewPopup() {
		
		// create xTemp Object & read template file
		$template = new XTemplate( $this->path . "/popup.html");
		
		// create data acces object
		$db = new DBAccess();
		$left = $db->selectOneByCause( $GLOBALS['pagepopupFullColArray'], PAGEPOPUP, array(PAGEPOPUP_TYPE => PAGEPOPUP_TYPE_LEFT, SQL_AND, PAGEPOPUP_LANGUAGE => $this->lan));
		$right = $db->selectOneByCause( $GLOBALS['pagepopupFullColArray'], PAGEPOPUP, array(PAGEPOPUP_TYPE => PAGEPOPUP_TYPE_RIGHT, SQL_AND, PAGEPOPUP_LANGUAGE => $this->lan));
		$main = $db->selectOneByCause( $GLOBALS['pagepopupFullColArray'], PAGEPOPUP, array(PAGEPOPUP_TYPE => PAGEPOPUP_TYPE_MAIN, SQL_AND, PAGEPOPUP_LANGUAGE => $this->lan));
		$bottom = $db->selectOneByCause( $GLOBALS['pagepopupFullColArray'], PAGEPOPUP, array(PAGEPOPUP_TYPE => PAGEPOPUP_TYPE_BOTTOM, SQL_AND, PAGEPOPUP_LANGUAGE => $this->lan));
		
		if ($left != null && count( $left) > 0 && ! empty( $left[PAGEPOPUP_FILELINK])) {
			$template->assign( LEFTPOPUP, $left);
			$template->parse( POPUP . "." . LEFTPOPUP);
		}
		
		if ($right != null && count( $right) > 0 && ! empty( $right[PAGEPOPUP_FILELINK])) {
			$template->assign( RIGHTPOPUP, $right);
			$template->parse( POPUP . "." . RIGHTPOPUP);
		}
		
		if ($main != null && count( $main) > 0 && ! empty( $main[PAGEPOPUP_FILELINK])) {
			
			if (! isset( $_SESSION[SESSION_VIEW_COUNT])) {
				$_SESSION[SESSION_VIEW_COUNT] = 0;
			} else {
				$_SESSION[SESSION_VIEW_COUNT] = $_SESSION[SESSION_VIEW_COUNT] + 1;
			}
			
			if ($_SESSION[SESSION_VIEW_COUNT] == 0 || $_SESSION[SESSION_VIEW_COUNT] % 25 == 0) {
				$template->assign( MAINPOPUP, $main);
				$template->parse( POPUP . "." . MAINPOPUP);
			}
		}
		
		if ($bottom != null && count( $bottom) > 0 && ! empty( $bottom[PAGEPOPUP_FILELINK])) {
			$template->assign( BOTTOMPOPUP, $bottom);
			$template->parse( POPUP . "." . BOTTOMPOPUP);
		}
		
		// return page
		$template->parse( POPUP);
		return $template->out( POPUP);
	}

	/**
	 * get website information
	 * @return Ambigous <string, NULL, $array, multitype:unknown >
	 */
	public function getWebInfo() {
		
		// create data acces object
		$db = new DBAccess();
		$website = $db->selectOneByCause( $GLOBALS['webinfoFullColArray'], WEBINFO, array(WEBINFO_LANGUAGE => $this->lan));
		
		if (empty( $website[WEBINFO_HEADERSCRIPT])) {
			$website[WEBINFO_HEADERSCRIPT] = " ";
		}
		if (empty( $website[WEBINFO_BODYSCRIPT])) {
			$website[WEBINFO_BODYSCRIPT] = " ";
		}
		if (empty( $website[WEBINFO_BACKLINK])) {
			$website[WEBINFO_BACKLINK] = " ";
		}
		
		return $website;
	}

	/**
	 * get organzation information
	 * @return Ambigous <string, NULL, $array, multitype:unknown >
	 */
	public function getContactInfo() {
		
		// create data acces object
		$db = new DBAccess();
		$info = $db->selectOneByCause( $GLOBALS['organinfoFullColArray'], ORGANINFO, array(ORGANINFO_LANGUAGE => $this->lan));
		
		return $info;
	}

	/**
	 */
	function __destruct() {
		
		// TODO - Insert your code here
	}
}

?>